class ChartController < ApplicationController
	unloadable

	# menu_item :issues, :only => [:issues_chart]

	before_filter :find_issues_by_tracker, :only => [:issues_chart]

	helper IssuesHelper
	helper :queries
  	include QueriesHelper
  	include IssuesHelper
  	include SortHelper

	def issues_chart
		retrieve_query
	end

	def find_issues_by_tracker

	    find_optional_project

		if params[:query_id] and Query.find(params[:query_id])
			query = Query.find(params[:query_id])
			query.add_filter("status_id", "*", [""])
			@issues_by_tracker = query.issues(:include => [:tracker]).group_by(&:tracker)
	    elsif !@project.nil?
	        ids = [@project.id]
	        ids += @project.descendants.active.visible.collect(&:id)
	        @issues_by_tracker= Issue.visible.find(:all, :include => [:project, :tracker], :conditions => ["#{Project.table_name}.id IN (?)", ids]).group_by(&:tracker)
	    else
	        @issues_by_tracker= Issue.visible.find(:all, :include => [:tracker]).group_by(&:tracker)
	    end

	rescue ActiveRecord::RecordNotFound
	    render_404
	end

	def find_optional_project
        @project = Project.find(params[:project_id]) unless params[:project_id].blank?
        deny_access unless User.current.allowed_to?(:view_issues, @project, :global => true)
    rescue ActiveRecord::RecordNotFound
        render_404
    end

end
